<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, ContractOrder, Config};
use think\Db;
use app\facade\Redis;

class MatchHall extends Command
{
    protected function configure()
    {
        $this->setName('MatchHall')->setDescription('合约订单超时自动完成或取消');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/rskj/think MatchHall'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        $config  = (new Config())->where('key', 'contracts')->value('value');
        $config = json_decode($config, true);
        if (empty($config['payment'])) {
            $times = 2;
        } else {
            $times = $config['payment'];
        }
        $matchtime = date('Y-m-d H:i:s', time() - $times * 3600);
        (new ContractOrder())->where(['status' => [1, 2]])->where('created|paytime', '<', $matchtime)->chunk(100, function ($list) use ($matchtime) {
            foreach ($list as $lists) {
                //未付款订单取消
                if ($lists['status'] == 1 && $lists['created'] < $matchtime) {
                    Db::startTrans();
                    try {
                        (new Users())->where(['id' => $lists['user_id']])->update(['status' => 1]);
                        (new ContractOrder())->where(['id' => $lists['id']])->update(['status' => 4]);
                        (new ContractSell())->where(['id' => $lists['matchid'], 'status' => 2])->update(['status' => 1]);
                        Db::commit();
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        Db::rollback();
                    }
                    echo '自动取消成功';
                }
                //未确认订单完结
                if ($lists['status'] == 2 && $lists['paytime'] < $matchtime) {
                    $sell = (new ContractSell())->where('id', $lists['matchid'])->find();
                    //卖家收益计算
                    $money = $sell['price'] - $sell['buyprice'];
                    Db::startTrans();
                    try {
                        (new ContractOrder())->where(['id' => $lists['id']])->update(['status' => 3, 'income_status' => 2]);
                        (new ContractSell())->where(['id' => $lists['matchid'], 'status' => 2])->update(['status' => 3]);
                        if ($money > 0) {
                            (new Users())->where('id', $sell['user_id'])->setInc('cincome', $money);
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        Db::rollback();
                    }
                    echo '自动完结成功';
                }
            }
        });
    }
}
